package cn.hunter.ascii;

import cn.hunter.ascii.image.ImageAsciiCache;
import cn.hunter.ascii.image.character.BestCharacterFitStrategy;
import cn.hunter.ascii.image.character.ColorSquareErrorFitStrategy;
import cn.hunter.ascii.image.character.StructuralSimilarityFitStrategy;
import cn.hunter.ascii.image.converter.AsciiToImageConverter;
import cn.hunter.ascii.image.converter.AsciiToStringConverter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Examples {
    public static void main(String[] args) throws IOException {

        // initialize caches
        ImageAsciiCache smallFontCache = ImageAsciiCache.create(new Font("Courier",
                Font.BOLD, 6));
        ImageAsciiCache mediumBlackAndWhiteCache = ImageAsciiCache.create(new Font(
                "Courier", Font.BOLD, 10), new char[] {'\\', ' ', '/'});
        ImageAsciiCache largeFontCache = ImageAsciiCache.create(new Font("Courier",
                Font.PLAIN, 16));

        // load image
        BufferedImage portraitImage = ImageIO.read(new File("image-ascii/examples/portrait.png"));

        // initialize algorithms
        BestCharacterFitStrategy squareErrorStrategy = new ColorSquareErrorFitStrategy();
        BestCharacterFitStrategy ssimStrategy = new StructuralSimilarityFitStrategy();

        // initialize converters
        AsciiToImageConverter imageConverter = new AsciiToImageConverter(
                smallFontCache, squareErrorStrategy);
        AsciiToStringConverter stringConverter = new AsciiToStringConverter(
                smallFontCache, ssimStrategy);

        // small font images, square error
        imageConverter.setCharacterCache(smallFontCache);
        imageConverter.setCharacterFitStrategy(squareErrorStrategy);
        ImageIO.write(imageConverter.convertImage(portraitImage), "png",
                new File("examples/portrait_small_square_error.png"));

        // medium font images, square error
        imageConverter.setCharacterCache(mediumBlackAndWhiteCache);
        imageConverter.setCharacterFitStrategy(squareErrorStrategy);
        ImageIO.write(imageConverter.convertImage(portraitImage), "png",
                new File("examples/portrait_medium_square_error.png"));

        // large font images, square error
        imageConverter.setCharacterCache(largeFontCache);
        imageConverter.setCharacterFitStrategy(squareErrorStrategy);
        ImageIO.write(imageConverter.convertImage(portraitImage), "png",
                new File("examples/portrait_large_square_error.png"));

        // small font images, ssim
        imageConverter.setCharacterCache(smallFontCache);
        imageConverter.setCharacterFitStrategy(ssimStrategy);
        ImageIO.write(imageConverter.convertImage(portraitImage), "png",
                new File("examples/portrait_small_ssim.png"));

        // medium font images, ssim error
        imageConverter.setCharacterCache(mediumBlackAndWhiteCache);
        imageConverter.setCharacterFitStrategy(ssimStrategy);
        ImageIO.write(imageConverter.convertImage(portraitImage), "png",
                new File("examples/portrait_medium_ssim.png"));

        // large font images, ssim
        imageConverter.setCharacterCache(largeFontCache);
        imageConverter.setCharacterFitStrategy(ssimStrategy);
        ImageIO.write(imageConverter.convertImage(portraitImage), "png",
                new File("examples/portrait_large_ssim.png"));
        System.out.println(stringConverter.convertImage(portraitImage));

    }
}
